<?php

Class AccountAction extends BaseAction {
	/*
	  +----------------------------------------------------------
	 * 显示管理员及注册用户的管理入口
	  +----------------------------------------------------------
	 */

	public function index() {
		$this->redirect("adminManage");
	}

	/*
	  +----------------------------------------------------------
	 * 管理员账户管理
	  +----------------------------------------------------------
	 */

	public function adminManage() {
		$Admin = D('Admin');
		import("Web.ORG.Util.Page");

		//搜索用户
		if ($_GET['keyword']) {
			$keyword = $_GET['keyword'];
			$data['admin_name'] = array('like', '%' . $keyword . '%');
		}

		//分页显示用户
		$count = $Admin->where($data)->count();
		$Page = new Page($count);
		$show = $Page->show();
		$admin = $Admin->where($data)->order('admin_id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();

		//分配数据到模板
		$this->assign('keyword', $keyword);
		$this->assign('pages', $show);
		$this->assign('admin', $admin);
		$this->display();
	}

	public function adminSave() {
		//显示修改页面
		$admin = D('Admin')->where('admin_id =' . $this->aid)->find();

		$this->assign('admin', $admin);
		$this->display();
	}

	public function adminSaves() {
		//保存修改项
		$data['admin_name'] = $_POST['admin_name'];
		if (!empty($_POST['password'])) {
			if (strlen($_POST['password']) < 6)
				$this->error("密码长度必须不小于六位");
			else
				$data['password'] = MD5($_POST['password']);
		}
		$result = D('Admin')->where('admin_id =' . $this->aid)->save($data);
		$this->success('修改成功!');
	}

	public function adminAdd() {
		//显示添加表单
		$this->assign("select", "adminAdd");
		$this->display("adminmanage");
	}

	public function adminAdds() {
		//添加新管理员账户
		$data['admin_name'] = trim($_POST['admin_name']);
		$data['password'] = MD5($_POST['password']);
		$data['power'] = $_POST['power'];
		empty($_POST['admin_name']) && $this->error('请填写用户名!');
		empty($_POST['password']) && $this->error('请填写密码!');
		if (strlen($_POST['password']) < 6)
			$this->error('密码必须大于6位!');
		$result = D('Admin')->data($data)->add();
		if ($result)
			$this->success('添加成功!');
		else
			$this->error('操作失败，请重试!');
	}

	public function adminDelete() {
		//删除单个管理员
		$admin_id = $_GET['admin_id'];
		if (!empty($admin_id)) {
			$admin = D('Admin')->where('admin_id=' . $admin_id)->delete();
			if ($admin)
				$this->success('删除成功!');
			else
				$this->error('操作失败，请重试!');
		}

		//删除多个管理员
		$checkbox = $_REQUEST['id'];
		$id = is_array($checkbox) ? implode(',', $checkbox) : $checkbox;
		if (!empty($checkbox)) {
			$admin = D('Admin')->execute('DELETE FROM ' . C("DB_PREFIX") . 'admin where admin_id ' . ' IN (' . $id . ')');
			if ($admin)
				$this->success('删除成功!');
			else
				$this->error('操作失败，请重试!');
		}
	}

	public function adminPower() {
		$aid = $_GET['admin_id'];
		$admin = D('Admin')->where('admin_id=' . $aid)->find();
		if (1 == $admin['power'])
			$admin['super'] = true;

		$this->assign("admin", $admin);
		$this->assign("select", "adminPower");
		$this->display("adminmanage");
	}

	public function adminPowers() {
		$admin = D('Admin')->where('admin_id =' . $_POST['aid']);
		if (1 == $_POST['power'])
			$admin->setField('power', '1');
		else
			$admin->setField('power', '0');

		$this->success('修改成功!');
	}

	/*
	  +----------------------------------------------------------
	 * 注册用户账号管理
	  +----------------------------------------------------------
	 */

	//显示所有用户 可根据手机/邮箱查找用户
	public function userManage() {
		$User = D('User');
		import("Web.ORG.Util.Page");
		if (1 == $this->power)
			$super = true;


		//搜索用户

		$type = $_GET['type'];
		if (!empty($type)) {
			$keyword = $_GET['keyword'];
			if ($keyword) {
				switch ($type) {
					case 'school':
						$map['school_abbr'] = $keyword;
						$result = D('School')->where($map)->find();
						$data['school_id'] = array('like', '%' . $result['school_id'] . '%');
						break;
					case 'email':
						$data['email'] = array('like', '%' . $keyword . '%');
						break;
					case 'phone':
						$data['mobilephone'] = array('like', '%' . $keyword . '%');
						break;
				}
			}
		}

		//分页显示用户
		$count = $User->count();
		$Page = new Page($count);
		$show = $Page->show();
		$user = $User->relation('school')->where($data)
			->order('user_id desc')->limit($Page->firstRow . ',' . $Page->listRows)
			->select();

		//分配数据到模板
		$this->assign('super', $super);
		$this->assign('pages', $show);
		$this->assign('user', $user);
		$this->display();
	}

	//修改用户资料
	public function userEdit() {
		$uid = $_GET['user_id'];
		$user = D('User')->relation('school')->where('user_id =' . $uid)->find();
		$data['school_id'] = array('neq', $user['school_id']);
		$data['status'] = 1;
		$school = D('School')->where($data)->select();

		$this->assign('school', $school);
		$this->assign('user', $user);
		$this->assign('select', 'userEdit');
		$this->display('usermanage');
	}

	public function userEdits() {
		$data['user_id'] = $_POST['user_id'];
		$data['email'] = trim($_POST['email']);
		$data['mobilephone'] = $_POST['mobilephone'];
		$data['status'] = $_POST['status'];

		if (!empty($_POST['password'])) {
			if (strlen($_POST['password']) < 6)
				$this->error("密码长度必须不小于六位");
			else
				$data['password'] = MD5($_POST['password']);
		}
		if (!Model::regex($data['email'], "email"))
			$this->error("邮箱格式错误");
		if (!empty($_POST['school']))
			$data['school_id'] = $_POST['school'];

		D('User')->save($data);
		$this->success('修改成功!');
	}

	//显示用户帖子
	public function tweetsShow() {
		import("Web.ORG.Util.Page");
		$Tweets = D('Tweets');
		$uid = $_GET['user_id'];

		$user = M("User")->find($uid);
		//分页显示微博
		$count = $Tweets->where('user_id =' . $uid)->count();
		$Page = new Page($count);
		$show = $Page->show();
		$result = $Tweets->relation(array('school', 'location'))
				->where('user_id =' . $uid)->order('tweets_id desc')
				->limit($Page->firstRow . ',' . $Page->listRows)->select();
		//数据分配模板
		$this->assign('pages', $show);
		$this->assign('result', $result);
		$this->assign("User", $User);
		$this->assign('select', 'tweetsShow');
		$this->display('usermanage');
	}

	//屏蔽/恢复用户的帖子
	public function tweetsDispose() {
		$act = $_POST['act'];
		$checkbox = $_POST['id'];
		empty($checkbox) && $this->error('请选操作对象!');
		$id = is_array($checkbox) ? implode(',', $checkbox) : $checkbox;

		//根据操作选择屏蔽/恢复
		switch ($act) {
			case 'hide':
				$sql = 'UPDATE ' . C("DB_PREFIX") . 'tweets SET status=0  where tweets_id ' . ' IN (' . $id . ')';
				break;
			case 'show':
				$sql = 'UPDATE ' . C("DB_PREFIX") . 'tweets SET status=1 where tweets_id ' . ' IN (' . $id . ')';
				break;
		}
		$tweets = D('Tweets')->execute($sql);
		if ($tweets)
			$this->success('操作成功!');
		else
			$this->error('操作失败，请重试!');
	}

	//显示用户评论
	public function commentShow() {
		import("Web.ORG.Util.Page");
		$Comment = D('Comment');
		$uid = $_GET['user_id'];
		$user = M("User")->find($uid);
		//分页显示微博
		$count = $Comment->where('user_id =' . $uid)->count();
		$Page = new Page($count);
		$show = $Page->show();
		$result = $Comment->relation(array('user'))
				->where('user_id =' . $uid)->order('comment_id desc')
				->limit($Page->firstRow . ',' . $Page->listRows)->select();

		//数据分配模板
		$this->assign('pages', $show);
		$this->assign('result', $result);
		$this->assign("User", $User);
		$this->assign('select', 'commentShow');
		$this->display('usermanage');
	}

	//屏蔽/恢复用户的评论
	public function commentDispose() {
		$act = $_POST['act'];
		$checkbox = $_POST['id'];
		empty($checkbox) && $this->error('请选操作对象!');
		$id = is_array($checkbox) ? implode(',', $checkbox) : $checkbox;

		//根据操作选择屏蔽/恢复
		switch ($act) {
			case 'hide':
				$sql = 'UPDATE ' . C("DB_PREFIX") . 'comment SET status=0  where comment_id ' . ' IN (' . $id . ')';
				break;
			case 'show':
				$sql = 'UPDATE ' . C("DB_PREFIX") . 'comment SET status=1 where comment_id ' . ' IN (' . $id . ')';
				break;
		}
		$comment = D('Comment')->execute($sql);
		if ($comment)
			$this->success('操作成功!');
		else
			$this->error('操作失败，请重试!');
	}

	//可选冻结时间 日/周/月/年
	public function userFreeze() {
		$uid = $_GET['user_id'];
		$this->assign('user_id', $uid);
		$this->assign("select", "userFreeze");
		$this->display("usermanage");
	}

	//冻结账户
	public function userFreezes() {
		$uid = $_POST['user_id'];
		$deadline = $_POST['deadline'];
		$backurl = $_POST['backurl'] ? $_POST['backurl'] : $_SERVER["HTTP_REFERER"];
		if ($uid && $deadline) {
			$User = D('User')->where('user_id =' . $uid);
			switch ($deadline) {
				case 'days':
					$User->setField(array('deadline', 'status'), array(time() + (3 * 24 * 3600), 0));
					$this->success('操作成功!', $backurl);
					break;
				case 'week':
					$User->setField(array('deadline', 'status'), array(time() + (7 * 24 * 3600), 0));
					$this->success('操作成功!', $backurl);
					break;
				case 'month':
					$User->setField(array('deadline', 'status'), array(time() + (30 * 24 * 3600), 0));
					$this->success('操作成功!', $backurl);
					break;
				case 'year':
					$User->setField(array('deadline', 'status'), array(time() + (360 * 24 * 3600), 0));
					$this->success('操作成功!', $backurl);
					break;
				default :$this->error('请选择冻结期限');
			}
		} else {
			$this->error('操作失败，请重试!');
		}
	}

	//解冻账户
	public function userThaw() {
		$uid = $_REQUEST['user_id'];
		if ($uid) {
			D('User')->where('user_id =' . $uid)->setField(array('deadline', 'status'), array(0, 1));
			redirect($_SERVER["HTTP_REFERER"]);
		}
	}

	//删除用户
	public function userDelete() {
		//删除单个用户
		$i = 0;
		$user_id = $_GET['user_id'];
		if (!empty($user_id)) {
			$userDelete = D('User')->delete($user_id);
			$tweets = D('Tweets')->where('user_id =' . $user_id)->select();
			foreach ($tweets as $key => $value) {
				$map[$key] = $tweets[$key]['tweets_id'];
			}
			$data['tweets_id'] = array('in', $map);
			$commentDelete = D('Comment')->where($data)->delete();
			$tweetsDelete = D('Tweets')->where('user_id =' . $user_id)->delete();

			if ($userDelete && $commentDelete && $tweetsDelete)
				$this->success('删除成功!');
			else
				$this->error('操作失败，请重试!');
		}

		//删除多个用户
		$checkbox = $_REQUEST['id'];
		$count = count($checkbox);
		if (!empty($checkbox)) {
			foreach ($checkbox as $id) {
				$userDelete = D('User')->delete($id);
				$tweets = D('Tweets')->where('user_id =' . $id)->select();
				foreach ($tweets as $key => $value) {
					$map[$key] = $tweets[$key]['tweets_id'];
				}
				$data['tweets_id'] = array('in', $map);
				$commentDelete = D('Comment')->where($data)->delete();
				$tweetsDelete = D('Tweets')->where('user_id =' . $id)->delete();
				if ($userDelete && $commentDelete && $tweetsDelete)
					$i++;
			}
			if ($i == $count) {
				$this->success('操作成功!');
			} else {
				$this->error('操作失败!');
			}
		}
	}

}

?>
